package com.renwei.yunlong.activity.work;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.CommonBean;
import com.renwei.yunlong.bean.ComplainDealBean;
import com.renwei.yunlong.event.ComplainPageRefreshEvent;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.view.SimpleOptionView;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

/**
 * 责任人
 */
public class ComplainPersonActivity extends BaseActivity implements TextWatcher, View.OnClickListener {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.tv_title_name)
    TextView tvTitleName;
    @Bind(R.id.tv_user_name)
    TextView tvUserName;
    @Bind(R.id.iv_enter)
    ImageView ivEnter;
    @Bind(R.id.rl_title)
    LinearLayout rlTitle;
    @Bind(R.id.tv_input_title)
    TextView tvInputTitle;
    @Bind(R.id.et_input_content)
    EditText etInputContent;
    @Bind(R.id.tv_input_number)
    TextView tvInputNumber;
    @Bind(R.id.btn_confirm)
    Button btnConfirm;
    private String newRequestId;
    private String dealEmployeeId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_close);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        newRequestId = getIntent().getStringExtra("newRequestId");

        initView();
        initData();
    }

    private void initView() {
        simpleTileView.setTitle("确认责任人");
        tvTitleName.setText("责任人");
        tvUserName.setHint("请选择（必选）");
        tvInputTitle.setText("确认原因");

        etInputContent.addTextChangedListener(this);

        btnConfirm.setOnClickListener(this);
        rlTitle.setOnClickListener(this);
    }


    private void initData() {
        ServiceRequestManager.getManager().getComplainDealPerson(this, newRequestId, this);
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1: // 获取处理人
                ComplainDealBean complainDealBean = new Gson().fromJson(result, ComplainDealBean.class);
                if (complainDealBean.getMessage().getCode()==200){
                    ComplainDealBean.RowsBean bean = complainDealBean.getRows();
                    tvUserName.setText(bean.getComplainUserName());
                    dealEmployeeId = bean.getComplainUserId();
                }
                break;
            case 2:
                CommonBean commonBean = new Gson().fromJson(result, CommonBean.class);
                if (commonBean.getMessage().getCode()==200){
                    EventBus.getDefault().post(new ComplainPageRefreshEvent());
                    showCenterInfoMsg("操作成功");
                    finish();
                }else{
                    showCenterInfoMsg(commonBean.getMessage().getMessage());
                }
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        int length = charSequence.length();
        tvInputNumber.setText(length + "");
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle bundle = null;
            if (data != null) {
                bundle = data.getExtras();
            }
            if (requestCode == 222) {
                // 筛选重复
                dealEmployeeId = bundle.getString("employeeId");
                String dealName = bundle.getString("name");

                tvUserName.setText(dealName);
            }
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_title:
                PersonSelectActivity.OpenActivity(this, PersonSelectActivity.FROM_DEAL, 222);
                break;
            case R.id.btn_confirm:
                if (TextUtils.isEmpty(dealEmployeeId)){
                    showCenterInfoMsg("请选择责任人");
                }else{
                    confrimPerson();
                }
                break;
        }
    }

    private void confrimPerson() {
        HashMap<String, String> map = new HashMap<>();
        if ("1".equals(companyType)) {
            map.put("currentUserId", ownerBean.getRows().getEmployeeId());
        } else {
            map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
        }
        map.put("newRequestId", newRequestId);
        map.put("complainUserId", dealEmployeeId);
        map.put("commitSuggest", etInputContent.getText().toString());

        ServiceRequestManager.getManager().setComplainDealPerson(this, JsonMapUtil.mapToJson(map), this);
    }

    public static void OpenActivity(Context context, String newRequestId) {
        Intent intent = new Intent(context, ComplainPersonActivity.class);
        intent.putExtra("newRequestId", newRequestId);
        context.startActivity(intent);
    }
}
